<?php
class project extends MY_Controller{
	
	public $title,$id_member;
	
	function project(){
		parent::__construct();
		$this->validateCookie();
		$this->id_member = get_cookie('member');
	}
	
	function index(){
		$this->projectPage();
//		echo $this->id_member;
	}
	
	function projectPage(){
		$this->title = "Project";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		$this->load->model("albumMod");
    	$query = $this->albumMod->get_records($this->id_member,0);
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/project/main',array('query'=>$query),true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/project/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	/*
	 * ------------------------------------------------------------------------------------------------------- show
	 */
	function showAlbum(){
		$this->load->model("albumMod");
    	$query = $this->albumMod->get_records($this->id_member,0);
    	    	
		$this->load->view('theme/default/dashboard/project/album/showAlbum',array('query'=>$query));
//		$this->load->view('theme/default/dashboard/project/album/showAlbum');
	}
	
	function showImages(){
		$id_album = $this->uri->segment(3,0);
//		echo $id_album;
		$this->load->model("imagesMod");
//		$query = array('all'=>$this->imagesMod->get_album($id_album,0),'thumbs'=>$this->imagesMod->getThumbs($id_album));
    	$query = $this->imagesMod->get_album($this->id_member,$id_album,0);
//    	$thumbs = $this->imagesMod->getThumbs($id_album); 
//    	echo $query;
//		$this->load->view('theme/default/dashboard/project/showAlbum',array('query'=>$query),true);
		$this->load->view('theme/default/dashboard/project/images/showImages',array('query'=>$query));
//		$this->load->view('theme/default/dashboard/project/showImages');
	}
	
	function showCommentAlbum(){
		$this->load->model("albumComMod");
		$id = $this->uri->segment(3,0);
//		echo $id;
    	$query = $this->albumComMod->get($id);
    	
		$this->load->view('theme/default/dashboard/project/album/showCommentAlbum',array('query'=>$query));
//		$this->load->view('theme/default/dashboard/project/album/showCommentAlbum');
	}
	
	function showCommentImages(){
		$this->load->model("imagesComMod");
		$id = $this->uri->segment(3,0);
//		echo $id;
    	$query = $this->imagesComMod->get($id);
    	
		$this->load->view('theme/default/dashboard/project/images/showCommentImages',array('query'=>$query));
//		$this->load->view('theme/default/dashboard/project/images/showCommentAlbum');
	}
	
	function detileShowImages(){
		$id_images = $this->uri->segment(4,0);
//		echo $id_album;
		$this->load->model("imagesMod");
    	$query = $this->imagesMod->get($id_images);
//    	echo $query;
//		$this->load->view('theme/default/dashboard/project/showAlbum',array('query'=>$query),true);
		$this->load->view('theme/default/dashboard/project/images/detileShowImages');
//		$this->load->view('theme/default/dashboard/project/showImages');
	}
	
	/*
	 * ------------------------------------------------------------------------------------------------------- add 
	 */
	function addImages(){
		
		
		$this->title = "Add Images";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		$this->load->model("imagesMod");
    	$query = $this->imagesMod->get_albumName();
    	
//		$this->load->view('theme/default/dashboard/project/images/addImages',array('query'=>$query));
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/project/images/addImages',array('query'=>$query),true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/project/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	function addImagesTrue1(){
		//$this->load->model("imagesMod");
    	//$query = $this->imagesMod->get_albumName();
    	
    	$this->uri->segment(3,0);
//		$this->load->view('theme/default/dashboard/project/images/addImages',array('query'=>$query));
		$this->load->view('theme/default/dashboard/project/images/addImagesTrue');
	}
	
	function addImagesTrue(){
		$this->title = "Add Images";
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/project/images/addImagesTrue','',true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/project/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	function addAlbum(){
		$this->load->view('theme/default/dashboard/project/album/addAlbum');
	}
	
	function addEditAlbum(){
		 $id = $this->uri->segment(3,0);
        $this->load->model('albumMod');
        $query = $this->albumMod->get($this->id_member,$id);
        $this->load->view('theme/default/dashboard/project/album/editAlbum',array('query'=>$query));
	}
	
	function addEditImages(){
		$id = $this->uri->segment(3,0);
        $this->load->model('imagesMod');
        $query = $this->imagesMod->get($id);
        $this->load->view('theme/default/dashboard/project/images/editImages',array('query'=>$query));
	}
	/*
	 * ------------------------------------------------------------------------------------------------------- load
	 */
	function loadThumbs(){
        $id = $this->uri->segment(3,0);
//      	echo $id;
        $this->load->model('imagesMod');
        $query = $this->imagesMod->getThumbs();
        $this->load->view('theme/default/dashboard/project/images/loadThumbs',array('query'=>$query));
//        $this->load->view('theme/default/dashboard/project/images/loadThumbs',$query);
    }
	
    function loadThumbsAlbum(){
        $id = $this->uri->segment(3,0);
//      	echo $id;
        $this->load->model('imagesMod');
        $query = $this->imagesMod->getThumbs();
        $this->load->view('theme/default/dashboard/project/album/loadThumbs',array('query'=>$query));
//        $this->load->view('theme/default/dashboard/project/images/loadThumbs',$query);
    }
	
	function loadImages(){
        $id = $this->uri->segment(3,0);
//      	echo $id;
        $this->load->model('imagesMod');
        $query = $this->imagesMod->getImages();
        $this->load->view('theme/default/dashboard/project/images/loadImages',array('query'=>$query));
//        $this->load->view('theme/default/dashboard/project/images/loadThumbs',$query);
    }
    
    /*
     * ------------------------------------------------------------------------------------------------------- open
     */
	function openAlbum(){
		$id_album = $this->uri->segment(3,0);
		$this->title = $id_album;
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		/*
		 * add query
		 */
		$id_album = $this->uri->segment(3,0);
		$this->load->model('imagesMod');
		$query = $this->imagesMod->get_album($this->id_member,$id_album,0);
		
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/project/album/openAlbum',array('query'=>$query),true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/project/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
    
	function openImages(){
		$id_album = $this->uri->segment(4,0);
		$this->title = $id_album;
		
		$this->config->load("raw");
		
		/*
		 * load header
		 */
		$header = $this->config->item("header");
		$header = $this->load->view("header",$header,true);
		/*
		 * add query
		 */
		$id_album = $this->uri->segment(3,0);
		$this->load->model('imagesMod');
		$query = $this->imagesMod->get_album($this->id_member,$id_album,0);
		
		
		/*
		 * load content
		 */
		$content = $this->config->item("content");
//		$content['lpanel'] = $this->load->view('restaurant/profile/theme/default/lpanel','',true);
		$content['main'] = $this->load->view('theme/default/dashboard/project/images/openImages','',true);
		$content['rpanel'] = $this->load->view('theme/default/dashboard/project/rpanel','',true);
		$con = $this->load->view("content",$content,true);
		
		/*
		 * load footer
		 */
		$footer = $this->config->item("footer");
		$footer = $this->load->view("footer",$footer,true);
		
		/*
		 * finalize view
		 */
		$page = $this->config->item("page");
		$page['title'] = $page['title']." - ".$this->title;
		$this->finalizeView(array(
			'title'=>$page['title'],
    		'jscript'=>js_script($page['jscript_default']),
    		'css'=>css_script($page['css_default']),
			'header'=>$header,
//			'lpanel'=>$lpanel,
    		'content'=>$con,
//			'rpanel'=>$rpanel,
			'footer'=>$footer,
			'layout_width'=>950,
// 			'nav'=>$nav
		));
	}
	
	/*
	 * ------------------------------------------------------------------------------------------------------- save 
	 */
	
	

	/**
	 * @return unknown_type
	 */
	function saveImages(){
		$this->load->model("imagesMod");
		$id_album = $this->input->post("album");
		$name = $this->input->post("name");
		$id_images = $this->imagesMod->generateID($name);
				
		$picture_tmp = $_FILES['images']['tmp_name'];
		$picture_name = $_FILES['images']['name'];
		$compress = 400;
		$thumbWidth = 120;
		$images_type = $_FILES['images']['type'];

		$tag = $this->input->post("tag");
		$desc = $this->input->post("description");
		
			$image = "";
			$newData = array(      		
	        	"id_images" => $id_images ,
				"id_album" => $id_album,
				"id_member" => $this->id_member,
				"name" => $name,
				"images" => $this->images($picture_tmp,$compress,$picture_name),
				"thumbs" => $this->thumbs($picture_tmp,$thumbWidth,$picture_name),
				"type_images" => $images_type,
				"tag" => $tag,
	        	"description"	=> $desc,
	      	);
//			$this->imagesMod->do_upload();
			$this->imagesMod->Add($newData);
			redirect("project/openAlbum/$id_album");
//			echo "Data has been Save...;')";
		
		return true;
	}
	
	function saveAlbum(){
		$this->load->model("albumMod");
		
		//$id_member = $this->input->post("id_member");
		$name = $this->input->post("name");
		$tag = $this->input->post("tag");
		$desc = $this->input->post("description");
		
		if($name==""){
			$message = "Name is Undefined";
			return "<script>failure()</script>";
		} else {
			$newData = array(      		
	        	"id_member"		=> $this->id_member, 
				"cover"			=> "",
	        	"name"			=> $name,
	        	"tag"			=> $tag,
	        	"description"	=> $desc,
	      	);
		}
		$this->albumMod->Add($newData);
		//echo "Data has been Save...;')";
		return true;
	}
	
	function saveCommentAlbum(){
		$this->load->model('albumComMod');
		
		$id_album = $this->uri->segment(3,0);
		$id_member = $this->id_member;
		$comment = $this->input->post("commentAlbum");
		
		$data = array(
				'id_album' => $id_album,
				'id_member' => $id_member,
				'comment' => $comment
				);
		
		$this->albumComMod->Add($data);
		
	}
	
	function saveCommentImages(){
		$this->load->model('imagesComMod');
		
		$id_images = $this->uri->segment(3,0);
//		echo $id_images;
		$id_member = $this->id_member;
		$comment = $this->input->post("commentImages");
		
		$data = array(
				'id_images' => $id_images,
				'id_member' => $id_member,
				'comment' => $comment
				);
		
		$this->imagesComMod->Add($data);
		
	}
	
	/*
	 * ------------------------------------------------------------------------------------------------------- delete
	 */
	function deleteAlbum(){
		$this->load->model("albumMod");
		$this->load->model("imagesMod");
//		$this->load->model("imagesComMod");
		$id = $this->uri->segment(3,0);
		$this->imagesMod->removeCommentImages($id);
		$table_images = "images";
		$comment = "comment_album";
		$this->albumMod->removeAlbum($table_images,$comment,$id);
		return true;
	}
	
	function deleteImages(){
		$this->load->model("imagesMod");
		$id = $this->uri->segment(3,0);
//		echo $id;
		$comment = "comment_images";
		$this->imagesMod->removeImages($comment,$id);
		return true;
	}
	
	/*
	 * edit
	 */	
	function saveEditImages(){
		$this->load->model('imagesMod');
		$id = $this->uri->segment(3,0);
		$name = $this->input->post('name');
		$tag = $this->input->post('tag');
		$description = $this->input->post('description');
		print_r($_POST);
		$data = array(
			'name' => $name,
			'tag' => $tag,
			'description' => $description,
		);
		echo $data;
		
		$this->imagesMod->update($id,$data);
		return true;
	}
	
	function saveEditAlbum(){
		$this->load->model('albumMod');
		$id = $this->uri->segment(3,0);
		$name = $this->input->post('name');
		$tag = $this->input->post('tag');
		$description = $this->input->post('description');
		$data = array(
			'name' => $name,
			'tag' => $tag,
			'description' => $description,
		);
		
		$this->albumMod->update($id,$data);
		return true;
	}
	
	function makeCover(){
		$this->load->model('imagesMod');
		$this->load->model('albumMod');
		$id_album = $this->uri->segment(3,0);
		$id_images = $this->uri->segment(4,0);
		$data = array(
			'cover' => $id_images,
		);
		
		$this->albumMod->update($id_album,$data);
		
	}
	
	function images($picture_tmp,$compress,$picture_name){
		$pic = imagecreatefromjpeg($picture_tmp);
		$width_comp  = imagesx( $pic );
		$height_comp  = imagesy( $pic );
		if($width_comp<300){
			$create = $pic;
			$new_width_comp = $width_comp;
			$new_height_comp  = $height_comp;
			$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
			$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
			$pth_comp  = site_url("images/album/$picture_name");
			ob_start();
			$inp_comp  = imagejpeg( $tmp_img_comp  );
			$create = ob_get_contents();	
			return $create;	
		} else {
		$new_width_comp = $compress;
		$new_height_comp  = floor( $height_comp * ( $compress / $width_comp  ) );
		$tmp_img_comp  = imagecreatetruecolor( $new_width_comp , $new_height_comp  );
		$up_comp  = imagecopyresized( $tmp_img_comp , $pic, 0, 0, 0, 0, $new_width_comp , $new_height_comp , $width_comp , $height_comp  );
		$pth_comp  = site_url("images/album/$picture_name");
		ob_start();
		$inp_comp  = imagejpeg( $tmp_img_comp  );
		$create = ob_get_contents(); 
		return $create;
		
		}
		
	
	}
	
	function thumbs($picture_tmp,$thumbwidth,$picture_name){
		$thumb = imagecreatefromjpeg($picture_tmp);
		$width = imagesx( $thumb );
		$height = imagesy( $thumb );
		$new_width = $thumbwidth;
		$new_height = floor( $height * ( $thumbwidth / $width ) );
		$tmp_img = imagecreatetruecolor( $new_width, $new_height );
		$up = imagecopyresized( $tmp_img, $thumb, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
		$pth = site_url("images/album/thumbs/$picture_name");
		ob_start();
		$inp = imagejpeg( $tmp_img );
		$c = ob_get_contents(); 
		return $c;
	}
}